package com.iot.ic.controller;

import com.iot.ic.base.BaseController;
import com.iot.ic.common.Response;
import com.iot.ic.common.ReturnCode;
import com.iot.ic.model.RefereeInfo;
import com.iot.ic.model.UserLogin;
import com.iot.ic.service.RefereeService;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@Api(description = "裁判信息相关接口")
@RestController
public class RefereeController extends BaseController {
	
	@Autowired
	private RefereeService refereeService;
	
	/**
	 * 裁判登录验证
	 * @param user
	 * @return
	 */
	@ApiOperation(value="裁判登录验证", notes="")
	@RequestMapping(value = "/referees/authorization", method = RequestMethod.POST)
	public Response userLogin(@RequestParam(value="refereeNo") String refereeNo,
			@RequestParam(value="refereePwd") String refereePwd) {
		Response response = new Response();
		RefereeInfo refereeInfo = new RefereeInfo();
		refereeInfo.setRefereeNo(refereeNo);
		refereeInfo.setRefereePwd(refereePwd);
		UserLogin userLogin = refereeService.login(refereeInfo);		
		response.setResult(userLogin);
		response.setInfos(ReturnCode.No_LoginOK);
		return response;
	}

	/**
	 * 添加裁判信息
	 * @param RefereeInfo
	 * @return
	 */
	@ApiOperation(value="添加裁判信息", notes="")
	@ResponseBody
	@RequestMapping(value="/users/{userId}/referees", method=RequestMethod.POST)
	public Response addRefereeInfo(@Valid@RequestBody RefereeInfo refereeInfo){
		Response response = new Response();
		int resultFlag = refereeService.addRefereeInfo(refereeInfo);
		if (resultFlag == 1) {
            response.setInfos(ReturnCode.No_AddRefereeOK);
        } else {
            response.setInfos(ReturnCode.Err_AddRefereeFailor);
        }
		return response;
	}


	/**
	 * 更改裁判信息
	 * @param RefereeInfo
	 * @return
	 */
	@ApiOperation(value="更改裁判信息", notes="")
	@ResponseBody
	@RequestMapping(value="/users/{userId}/referees/{refereeId}", method=RequestMethod.PUT)
	public Response updateRefereeInfo(@PathVariable("refereeId") int refereeId,@Valid@RequestBody RefereeInfo refereeInfo){
		Response response = new Response();
		refereeInfo.setRefereeId(refereeId);
		int resultFlag = refereeService.updateRefereeInfo(refereeInfo);
		if (resultFlag == 1) {
            response.setInfos(ReturnCode.No_UpdateRefereeOK);
        } else {
            response.setInfos(ReturnCode.Err_UpdateRefereeFailor);
        }
		return response;
	}

	/**
	 * 获得裁判信息列表
	 * @param RefereeInfo
	 * @return
	 */
	@ApiOperation(value="获得裁判信息列表", notes="")
	@ResponseBody
	@RequestMapping(value="/referees", method=RequestMethod.GET)
	public Response listRefereeInfos(@RequestParam(value="currentPage") int currentPage,
			@RequestParam(value="pageSize") int pageSize){
		Response response = new Response();
		RefereeInfo refereeInfo = new RefereeInfo();
		refereeInfo.setCurrentPage(currentPage);
		refereeInfo.setLength(pageSize);
		List<RefereeInfo> refereeList = refereeService.listRefereeInfos(refereeInfo);
		response.setResult(refereeList);
		return response;
	}
	
	
	/**
	 * 获得裁判信息列表
	 * @param RefereeInfo
	 * @return
	 */
	@ApiOperation(value="获得用户信息列表", notes="")
	@ResponseBody
	@RequestMapping(value="/user", method=RequestMethod.GET)
	public Response listUsers(@RequestParam(value="currentPage") int currentPage,
			@RequestParam(value="pageSize") int pageSize){
		Response response = new Response();
		RefereeInfo refereeInfo = new RefereeInfo();
		refereeInfo.setCurrentPage(currentPage);
		refereeInfo.setLength(pageSize);
		List<RefereeInfo> refereeList = refereeService.listUsers(refereeInfo);
		response.setResult(refereeList);
		return response;
	}
	
	
	/**
	 * 根据Id获得裁判信息
	 * @param RefereeInfo
	 * @return
	 */
	@ApiOperation(value="根据Id获得裁判信息", notes="")
	@ResponseBody
	@RequestMapping(value="/referees/{refereeId}", method=RequestMethod.GET)
	public Response getRefereeInfoById(@PathVariable("refereeId") int refereeId){
		Response response = new Response();
		RefereeInfo referee = refereeService.getRefereeInfo(refereeId);
		response.setResult(referee);
		return response;
	}

	/**
	 * 删除裁判信息
	 * @param RefereeInfo
	 * @return
	 */
	@ApiOperation(value="删除裁判信息", notes="")
	@ResponseBody
	@RequestMapping(value="/users/{userId}/referees/{refereeId}", method=RequestMethod.DELETE)
	public Response deleteRefereeInfos(@PathVariable("userId") int userId, @PathVariable("refereeId") int refereeId){
		Response response = new Response();
		int resultFlag = refereeService.deleteRefereeInfo(refereeId);
		if (resultFlag == 1) {
            response.setInfos(ReturnCode.No_DeleteRefereeOK);
        } else {
            response.setInfos(ReturnCode.Err_DeleteRefereeFailor);
        }
		return response;
	}


	/**
	 * 修改密码
	 * @param user
	 * @param oldPwd
	 * @return
	 */
	@ApiOperation(value="修改密码", notes="")
	@ResponseBody
	@RequestMapping(value = "/users/{userId}/pwd", method = RequestMethod.POST)
	public Response changePwd(@RequestParam(value="userPwd") String userPwd,
			@RequestParam(value="oldPwd") String oldPwd,@PathVariable("userId")int userId,HttpServletRequest request) {
		Response response=new Response();
		RefereeInfo referee = new RefereeInfo();
		referee.setRefereePwd(userPwd);
		referee.setRefereeId(userId);
		referee.setOldRefereePwd(oldPwd);
		int result = refereeService.changePwd(referee);
		if (result == 1) {
            response.setInfos(ReturnCode.No_Err);
        } else {
            response.setInfos(ReturnCode.Err_RequestFailed);
        }
		return response;
	}
	
	
}
